<?php
include_once("DataBase.php");
/*
 * Class designed to model the databases table 'Student'
 */
class Student{
    //Attributes
    
    //Unique identification given by the university
    private $carnet;
    //Students name
    private $name;
    //Students last name
    private $lastName;
    //Students phone number
    private $phone;
    
    //Methods
    
    /*
     * Returns an array of Student containing all the students present in the
     * database
     *
     * Return value: Array of Student containing all the students in the database
     */
    public static function getAllStudents(){
        $database = DataBase::getInstance();
        return $database->selectAllStudents(); 
    }
    
    //Builds an object without information in it
    public function __construct($carnet, $name, $lastName, $phone){
        $this->carnet = $carnet;
        $this->name = $name;
        $this->lastName = $lastName;
        $this->phone = $phone;
    }
    
    /*
     * Find in the database a specified student using its carnet
     *
     * Parameters: $carnet -> Unique identifier for the student
     * Return value: A Student object containing the information of the desired
     *               student or null if it was not found.
     */
     public static function findStudent($carnet){
        $database = DataBase::getInstance();
        return $database->selectStudent($carnet);        
     }
     
     /**
      * Delete the student contained in $this
      */
      public function deleteStudent(){
        $database = DataBase::getInstance();
        $database->deleteStudent($this->getCarnet());
      }
     
     /*
      * Subscribe a book loan to the student instance
      *
      * Parameters: $book -> Book object that represents the book to be loaned
      * Return: A string that represents the date the book must be returned
      */
    public function rentBook($book){
        $database = DataBase::getInstance();
        if(!$book->isAvailable())
            throw new Exception('El libro no esta disponible');
        
        return $database->makeLoan($this, $book);
    }
     
    /*
     * Updates the information contained in the instance, or creates a new entry
     * if the instance doesn't exist
     */
    public function save(){
        $database = DataBase::getInstance();
        $database->saveStudent($this);
    }
    //List of getters
    public function getCarnet(){return $this->carnet;}
    public function getName(){return $this->name;}
    public function getLastName(){return $this->lastName;}
    public function getPhone(){return $this->phone;}
    
    //List of setters
    public function setCarnet($a){$this->carnet = $a;}
    public function setName($a){$this->name = $a;}
    public function setLastName($a){$this->lastName = $a;}
    public function setPhone($a){$this->phone = $a;}
}
?>